package com.training;

class Computations extends Thread
{
	private int num,result;
	private boolean isComplete;

	public Computations(int num){
		this.num = num;
		System.out.println("Computations:Computations:"+num);
	}

	public synchronized void run() 	{
		System.out.println("Computations:run:"+num);
		result = num * 2;
		isComplete=true;
		notify();
	}

	public int getResult() {
		System.out.println("Computations:getResult:"+num+":OutSide While"); 
		while(!isComplete)
		{
			try{
				System.out.println("Computations:getResult:"+num+":Inside While");
				wait();
				System.out.println("Computations:getResult:"+num+":Inside While:After Wait");
			}
			catch(Exception e){
				e.printStackTrace();
			}
		}
		return result;
	}	
}

public class ThrNotifyWait {

	public static void main(String[] args) throws Exception{	
		Computations[] comp = new Computations[4];			
		for(int i =0;i<4 ;i++){
			comp[i] = new Computations(i);
			comp[i].start();
			comp[i].join();
		}			
		for(Computations c :comp)
			System.out.println(c.getResult());

	}

}
